package org.plex.core.imp;

import java.util.ArrayList;
import java.util.List;

import org.plex.core.TaskEndpointManager;
import org.plex.endpoints.EndpointInvokeResult;
import org.plex.endpoints.imp.EndpointInvokeResultImp;
import org.plex.model.TaskEndpoint;
import org.plex.model.TaskEndpointCategory;
import org.plex.model.imp.TaskEndpointCategoryImp;
import org.plex.model.imp.TaskEndpointImp;
import com.adobe.idp.dsc.clientsdk.ServiceClientFactory;
import com.adobe.idp.taskmanager.dsc.client.TaskManagerClientFactory;
import com.adobe.idp.taskmanager.dsc.client.endpoint.TaskEndpointException;
import com.adobe.idp.taskmanager.dsc.client.endpoint.TaskEndpointNotFound;
import com.adobe.idp.taskmanager.dsc.client.endpoint.TaskManagerEndpointClient;

public class TaskEndpointManagerImp implements TaskEndpointManager {

	private ServiceClientFactory serviceClientFactory;
	private TaskManagerEndpointClient taskManagerEndpointClient;

	public TaskEndpointManagerImp(ServiceClientFactory serviceClientFactory)
			throws NullPointerException {
		if (null == serviceClientFactory) {
			throw new NullPointerException();
		}
		this.setServiceClientFactory(serviceClientFactory);
		taskManagerEndpointClient = TaskManagerClientFactory
				.getEndpointClient(serviceClientFactory);
	}

	public TaskEndpoint getEndpointByName(String serviceName,
			String endpointName) {

		try {
			com.adobe.idp.taskmanager.dsc.client.endpoint.TaskEndpoint taskEndpoint = taskManagerEndpointClient
					.getEndpointByName(serviceName, endpointName);
			return new TaskEndpointImp(taskEndpoint);
		} catch (TaskEndpointException e) {
			e.printStackTrace();
			return null;
		}
	}

	public List<TaskEndpointCategory> getEndpointCategories() {
		try {
			com.adobe.idp.taskmanager.dsc.client.endpoint.TaskEndpointCategory[] categories = taskManagerEndpointClient
					.getCategories();
			List<TaskEndpointCategory> list = new ArrayList<TaskEndpointCategory>();
			for (com.adobe.idp.taskmanager.dsc.client.endpoint.TaskEndpointCategory category : categories) {
				list.add(new TaskEndpointCategoryImp(category));
			}

			return list;
		} catch (TaskEndpointException e) {
			e.printStackTrace();
			return null;
		}
	}

	public List<TaskEndpoint> getEndpointsFromIds(long[] endpointIds) {

		try {
			com.adobe.idp.taskmanager.dsc.client.endpoint.TaskEndpoint[] taskEndpoints = taskManagerEndpointClient
					.getEndpointsFromIds(endpointIds);
			List<TaskEndpoint> list = new ArrayList<TaskEndpoint>();
			for (com.adobe.idp.taskmanager.dsc.client.endpoint.TaskEndpoint taskEndpoint : taskEndpoints) {
				list.add(new TaskEndpointImp(taskEndpoint));
			}
			return list;
		} catch (TaskEndpointException e) {
			e.printStackTrace();
			return null;
		}

	}

	public TaskEndpoint getTaskEndpointByName(String serviceName,
			String endpointName) {
		try {
			com.adobe.idp.taskmanager.dsc.client.endpoint.TaskEndpoint taskEndpoint = taskManagerEndpointClient
					.getEndpointByName(serviceName, endpointName);
			return new TaskEndpointImp(taskEndpoint);

		} catch (TaskEndpointException e) {
			e.printStackTrace();
			return null;
		}
	}

	public List<TaskEndpoint> getTaskEndpointsForCategory(String categoryId) {

		try {
			com.adobe.idp.taskmanager.dsc.client.endpoint.TaskEndpoint[] taskEndpoints = taskManagerEndpointClient
					.getEndpointsForCategory(categoryId);
			List<TaskEndpoint> list = new ArrayList<TaskEndpoint>();
			for (com.adobe.idp.taskmanager.dsc.client.endpoint.TaskEndpoint taskEndpoint : taskEndpoints) {
				list.add(new TaskEndpointImp(taskEndpoint));
			}
			return list;
		} catch (TaskEndpointException e) {
			e.printStackTrace();
			return null;
		}

	}

	public EndpointInvokeResult invokeEndpoint(long endpointId) {

		try {
			com.adobe.idp.taskmanager.dsc.client.endpoint.EndpointInvokeResult result = taskManagerEndpointClient
					.invokeEndpoint(endpointId);
			return new EndpointInvokeResultImp(result);
		} catch (TaskEndpointNotFound e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (TaskEndpointException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	public EndpointInvokeResult invokeEndpointFromStartTask(long endpointId) {
		// TODO Auto-generated method stub
		try {
			com.adobe.idp.taskmanager.dsc.client.endpoint.EndpointInvokeResult endpointInvokeResult = taskManagerEndpointClient
					.invokeEndpointFromStartTask(endpointId);
			return new EndpointInvokeResultImp(endpointInvokeResult);
		} catch (TaskEndpointNotFound e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (TaskEndpointException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}

	}

	public void setServiceClientFactory(
			ServiceClientFactory serviceClientFactory) {
		this.serviceClientFactory = serviceClientFactory;
		this.taskManagerEndpointClient = TaskManagerClientFactory
				.getEndpointClient(serviceClientFactory);
	}

	public ServiceClientFactory getServiceClientFactory() {
		return serviceClientFactory;
	}

}
